package com.unswift;

import java.io.File;
import java.util.Set;

import com.unswift.annotation.api.Api;
import com.unswift.annotation.api.ApiField;
import com.unswift.annotation.api.ApiMethod;
import com.unswift.utils.ExceptionUtils;
import com.unswift.utils.FileUtils;
import com.unswift.utils.ObjectUtils;
import com.unswift.utils.StringUtils;

@Api(value="根据微服务原始项目生成freemarker模板", author="usnwift", date="2023-06-20", version="1.0.0")
public class GenericsCloudTemplate {

	@ApiField("微服务模板项目路径，此路径是可以直接导入eclipse运行的maven项目")
	private static final String originalPath="D:\\develop\\workspace-cloud\\unswift-cloud";
	
	@ApiField("微服务模板项目名称")
	private static final String originalProjectName="unswift-cloud";
	
	@ApiField("宇燕快速框架模板存放的路径-非jar包，目前还不支持jar包里运行此类")
	private static String templatePath="src\\main\\resources\\template\\project\\cloud";
	
	@ApiField("排除的项目文件")
	private static Set<String> excludeSet=ObjectUtils.asSet(".project",".gitignore",".settings","target",".git",".classpath");
	
	@ApiField("排除的文件")
	private static String[] excludeSuffixArray=new String[]{".css",".js",".eot",".svg",".ttf",".woff",".woff2",".gif",".png",".jpg"};
	
	
	public static void main(String[] args) {
		ExceptionUtils.loadingMessageXml(Thread.currentThread().getContextClassLoader().getResourceAsStream("quick-exception.xml"), "quick-exception.xml");
		File originalFile=new File(originalPath);
		File pomFile=new File(originalPath+"\\pom.xml");
		ExceptionUtils.trueException(!pomFile.exists(), "must.maven.project", "语言微服务");
		File[] fileList=originalFile.listFiles();
		String quickRootClassPath=Thread.currentThread().getContextClassLoader().getResource("").getFile();
		String quickRoot=quickRootClassPath.substring(1, quickRootClassPath.lastIndexOf("target")).replace("/", "\\");
		templatePath=quickRoot+templatePath;
		foreachListFile(fileList, "", originalFile.getName(), 1);
	}
	
	private static void foreachListFile(File[] fileList, String parentPath, String parentFileName, int depth){
		if(ObjectUtils.isEmpty(fileList)){
			return ;
		}
		String fileName, fileContent, templateFile;
		for (File file : fileList) {
			fileName = file.getName();
			if(depth<=2 && excludeSet.contains(fileName)){//排除的文件
				continue;
			}
			if(file.isDirectory()){
				foreachListFile(file.listFiles(), parentPath+fileName+"\\", fileName, depth+1);
			}else{
				fileContent=FileUtils.reader(file, "UTF-8");
				if(!StringUtils.endsWiths(file.getName(), excludeSuffixArray)){
					fileContent=replaceTemplateGetValue(fileContent, parentPath+fileName);
					if("pom.xml".equals(fileName) || fileName.endsWith("exception.xml")){
						fileContent=fileContent.replace("com.unswift.cloud", "${projectDomain}");
						fileContent=fileContent.replace(originalProjectName, "${projectName}");
					}else if(fileName.endsWith(".java")){
						fileContent=fileContent.replace("com.unswift.cloud", "${projectDomain}");
					}else if(fileName.endsWith("Mapper.xml")){
						fileContent=fileContent.replace("#", "${r'#'}");
					}
				}
				templateFile=templatePath+"\\"+parentPath+fileName;
				System.out.println("写入文件："+templateFile);
				FileUtils.writer(templateFile, fileContent, "UTF-8", false);
			}
		}
	}
	
	@ApiMethod(value="特换内容中的${...}为${r'${...}'}")
	private static String replaceTemplateGetValue(String content, String fileName){
		int index=content.indexOf("${");
		int first=0,last;
		StringBuilder template=new StringBuilder();
		while(index!=-1){
			last=content.indexOf("}", index+2);
			ExceptionUtils.trueException(last==-1, "file.content.get.value.end.symbol", fileName);
			last++;
			template.append(content.substring(first, index));
			template.append("${r'").append(content.substring(index, last)).append("'}");
			first=last;
			index=content.indexOf("${", first);
		}
		template.append(content.substring(first));
		return template.toString();
	}
}
